//
//  TalkfunDottedLine.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/1/25.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TalkfunDottedLine: UIView {
    let shapeLayer = CAShapeLayer()
    
    let path = CGMutablePath()
    var isAdd = false
    //MARK:- 绘制虚线
    func drawDashLine(strokeColor: UIColor, lineWidth: CGFloat = 1, lineLength: Int = 5, lineSpacing: Int = 5, isBottom: Bool = true) {
        
        shapeLayer.bounds = self.bounds
        shapeLayer.anchorPoint = CGPoint(x: 0, y: 0)
        shapeLayer.fillColor = UIColor.blue.cgColor
        shapeLayer.strokeColor = strokeColor.cgColor
        
        shapeLayer.lineWidth = lineWidth
        shapeLayer.lineJoin = CAShapeLayerLineJoin.round
        
        //每一段虚线长度 和 每两段虚线之间的间隔
        shapeLayer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]
        
        
        let y = isBottom == true ? self.layer.bounds.height - lineWidth : 0
        path.move(to: CGPoint(x: 0, y: y))
        path.addLine(to: CGPoint(x: self.layer.bounds.width, y: y))
        shapeLayer.path = path
        if isAdd == false {
            isAdd = true
            self.layer.addSublayer(shapeLayer)
        }
       
    }
}
